/*
* Copyright (c) 2016, Freescale Semiconductor, Inc.
* Copyright 2016-2019 NXP
* All rights reserved.
*
*
* SPDX-License-Identifier: BSD-3-Clause
*/

/*******************************************************************************
* Includes
******************************************************************************/
#include "lwip/opt.h"
#include "cpuloading.h"

#if LWIP_NETCONN

#include "opcua_task.h"
#include "lwip/netifapi.h"
#include "lwip/tcpip.h"
#include "netif/ethernet.h"
#include "enet_ethernetif.h"
#include "sdcard_fatfs_freertos.h"

#include "board.h"

#include "pin_mux.h"
#include "clock_config.h"
#include "fsl_gpio.h"
#include "fsl_iomuxc.h"
#include "snvs_hp_rtc.h"
#include "Modbus.h"
#include "parse_cfg_task.h"

/*******************************************************************************
* Definitions
******************************************************************************/



/*******************************************************************************
* Prototypes
******************************************************************************/
extern void ENET_DriverIRQHandler(void);
extern int snvs_hp_rtc(void);
extern int flexspi_nor_task(void);
extern void modbus_init(void);
extern void flexspi_nor_init(void);
extern void CRYPTO_InitHardware(void);
/*******************************************************************************
* Variables
******************************************************************************/

/*******************************************************************************
* Code
******************************************************************************/
void BOARD_InitModuleClock(void)
{
  const clock_enet_pll_config_t config = {.enableClkOutput = true, .enableClkOutput25M = false, .loopDivider = 1};
  CLOCK_InitEnetPll(&config);
}

void delay(void)
{
  volatile uint32_t i = 0;
  for (i = 0; i < 1000000; ++i)
  {
    __asm("NOP"); /* delay */
  }
}



/*!
* @brief Main function
*/
int main(void)
{   
  gpio_pin_config_t gpio_config = {kGPIO_DigitalOutput, 0, kGPIO_NoIntmode};
  
  BOARD_ConfigMPU();
  BOARD_InitPins();
  BOARD_BootClockRUN();
  BOARD_USDHCClockConfiguration();
  BOARD_InitDebugConsole();
  BOARD_InitModuleClock();
  
  //For Modbus used PIT
  /* Set PERCLK_CLK source to OSC_CLK*/
  CLOCK_SetMux(kCLOCK_PerclkMux, 1U);
  /* Set PERCLK_CLK divider to 1 */
  CLOCK_SetDiv(kCLOCK_PerclkDiv, 0U);
  
  /* No need to disable the DCache due to changed the fsl_dcp.c */
  CRYPTO_InitHardware();
  
  NVIC_SetPriority(BOARD_SD_HOST_IRQ, 5U);
  
  IOMUXC_EnableMode(IOMUXC_GPR, kIOMUXC_GPR_ENET1TxClkOutputDir, true);
  
  snvs_hp_rtc();
  
  GPIO_PinInit(GPIO1, 9, &gpio_config);
  GPIO_PinInit(GPIO1, 10, &gpio_config);
  /* pull up the ENET_INT before RESET. */
  GPIO_WritePinOutput(GPIO1, 10, 1);
  GPIO_WritePinOutput(GPIO1, 9, 0);
  delay();
  GPIO_WritePinOutput(GPIO1, 9, 1);

  parse_cfg_init();
  sdcard_fatfs_init();
  flexspi_nor_init();
  StartProfiler();
  vTaskStartScheduler();

  /* Will not get here unless a task calls vTaskEndScheduler ()*/
  return 0;
}

#endif
